<template>
  <div style="padding: 10px">
      <el-form :model="form" label-width="120px">
        <el-form-item label="用户名">
          <el-input v-model="form.username" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.nickName" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="form.age" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio v-model="form.sex" label="男">男</el-radio>
          <el-radio v-model="form.sex" label="女">女</el-radio>
          <el-radio v-model="form.sex" label="未知">未知</el-radio>
        </el-form-item>
        <el-form-item label="地址">
          <el-input type="textarea" v-model="form.address" style="width: 80%"></el-input>
        </el-form-item>
      </el-form>
    <el-button type="primary" @click="save">保存</el-button>

  </div>

</template>

<script>

import request from "../../utils/request";
import {defineComponent} from 'vue'
import {ElMessage} from 'element-plus'

export default {
  name: 'Home',
  components: {},
  data() {
    return {
      form: {},
      dialogVisible: false,
      search: '',
      currentPage: 1,
      pageSize: 10,
      total: 0,
      tableData: [],
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      request.get("/user", {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search
        }
      })
          .then(res => {
            this.tableData = res.data.records
            this.total = res.data.total
          })
    },
    save() {
      if (this.form.id) {   //更新
        request.put("/user", this.form).then(res => {
          if (res.code === '0') {
            this.load()
            ElMessage({
              showClose: true,
              type: "success",
              message: "修改成功",
              duration: 2000
            })
          } else {
            ElMessage({
              showClose: true,
              type: "error",
              message: "修改失败:" + res.msg,
            })
          }
        })
      }
    },
    handleDelete(id) {
      request.delete("/user/" + id).then(res => {
        if (res.code === '0') {
          ElMessage({
            showClose: true,
            type: "success",
            message: "删除成功",
            duration: 2000
          })
        } else {
          ElMessage({
            showClose: true,
            type: "error",
            message: "删除失败" + res.msg,
            duration: 2000
          })
        }
      })
      this.load()
    },

  }
}
</script>
